//
//  TYAttributedLabel+RAC.m
//  高仿暴走斗图
//
//  Created by key on 16/4/20.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "TYAttributedLabel+RAC.h"
#import "TYTextContainerInfo.h"

static NSString *kTYRACKey = @"HSYRACKey";

@implementation TYAttributedLabel (RAC)

- (RACSubject *)subject {
    
    return objc_getAssociatedObject(self, &kTYRACKey);
}

- (void)setSubject:(RACSubject *)subject {
    
    [self willChangeValueForKey:kTYRACKey];
    objc_setAssociatedObject(self, &kTYRACKey, subject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:kTYRACKey];
}


- (void)addTextStorangeArray:(NSArray *)array withWidth:(CGFloat)width {
    
    if (array.count == 0) {
        return;
    }
    
    [self.textContainer addTextStorageArray:array];
    self.textContainer = [self.textContainer createTextContainerWithTextWidth:width];
    self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMidY(self.frame), CGRectGetWidth(self.frame), self.textContainer.textHeight);
}


+ (RACSignal *)createArrtibutedLabelShowInParentView:(UIView *)parentView andDelegate:(id<TYAttributedLabelDelegate>)delegate text:(NSString *)text characterSpacing:(unichar)characterSpacing linesSpacing:(CGFloat)linesSpacing textColor:(UIColor *)textColor font:(UIFont *)font {
    
    TYTextContainer *textContainer = [NSObject createTextContainerWithCharacterSpacing:characterSpacing
                                                                       andLinesSpacing:linesSpacing
                                                                                  text:text
                                                                             textColor:textColor
                                                                                  font:font];
    
    CGRect rect = CGRectMake(0, 0, MAXFLOAT, MAXFLOAT);
    TYAttributedLabel *contentLabel = [NSObject createAttributedLabelForDelegate:delegate withTextContainer:textContainer frame:rect];
    [parentView addSubview:contentLabel];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
       
        [NSObject parseTextContainerForString:text withUsingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
            
            [subscriber sendNext:@(captureCount)];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}

- (TYAttributedLabel *)createArrtibutedLabelForDelegate:(id<TYAttributedLabelDelegate>)delegate text:(NSString *)text characterSpacing:(unichar)characterSpacing linesSpacing:(CGFloat)linesSpacing textColor:(UIColor *)textColor font:(UIFont *)font {
    
    TYTextContainer *textContainer = [NSObject createTextContainerWithCharacterSpacing:characterSpacing
                                                                       andLinesSpacing:linesSpacing
                                                                                  text:text
                                                                             textColor:textColor
                                                                                  font:font];
    CGRect rect = CGRectMake(0, 0, MAXFLOAT, MAXFLOAT);
    TYAttributedLabel *contentLabel = [NSObject createAttributedLabelForDelegate:delegate withTextContainer:textContainer frame:rect];
    
    RACSignal *siganl = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [NSObject parseTextContainerForString:text withUsingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
            
            [subscriber sendNext:@(captureCount)];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    
    [siganl subscribe:self.subject];
    
    return contentLabel;
}

+ (TYAttributedLabel *)createArrtibutedLabelForDelegate:(id<TYAttributedLabelDelegate>)delegate text:(NSString *)text characterSpacing:(unichar)characterSpacing linesSpacing:(CGFloat)linesSpacing textColor:(UIColor *)textColor font:(UIFont *)font {
    
    return [[self alloc] createArrtibutedLabelForDelegate:delegate text:text characterSpacing:characterSpacing linesSpacing:linesSpacing textColor:textColor font:font];
}

@end
